package com.price.sample.http.service

import com.price.httplib.base.BaseHttpApiService
import com.price.httplib.ssl.SSLHostnameVerifier
import com.price.httplib.ssl.SSLSocketFactoryCompat
import com.price.sample.http.interceptor.MockInterceptor
import com.price.sample.http.myinterface.WannaAndroidApi
import okhttp3.Interceptor
import okhttp3.OkHttpClient


/**
 * 设置服务器配置，比如域名，公共请求头，返回数据格式
 *  测试域名为https://www.wanandroid.com/ 的服务
 *
 */
object WanAndroidApiService : BaseHttpApiService() {

    override fun getServiceUrl(): String {
        return "https://www.wanandroid.com/"
    }

    override fun getInterceptor(): List<Interceptor> {
        val headerInterceptor=Interceptor { chain ->
            val builder = chain.request().newBuilder()
            builder.addHeader("Source", "source")
            chain.proceed(builder.build())
        }

        return listOf(headerInterceptor, MockInterceptor())
    }


    /**
     * 获取WannaAndroidApi 服务
     *
     */
    fun getService(): WannaAndroidApi {
        return getService(WannaAndroidApi::class.java)
    }

    override fun okHttpOtherSet(okHttpBuilder: OkHttpClient.Builder): OkHttpClient.Builder {
        val sslSocketFactory = SSLSocketFactoryCompat()
        okHttpBuilder.hostnameVerifier(SSLHostnameVerifier())
            .sslSocketFactory(sslSocketFactory, sslSocketFactory.MyTrustManager())
        return okHttpBuilder
    }
}

